define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/op/models'),
        utils = require('js/utils/tools'),
        app = Backbone.history;
    require('paginator');

    var BigShowListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
        },

        initialize: function (options) {
            console.log("bigshow js init");
            this.options = options;
            this.collection = new models.TemplateCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        doSearch: function () {
            var options = utils.getAllInput("#sidebar_right", true),
                searched = utils.getAllInput('#sidebar_right');
            if (searched.id) {
                var id = Number(searched.id);
                if (isNaN(id)) {
                    options.name = {
                        '$like': searched.id
                    };
                    delete options.id;
                }
            }
            utils.saveSearched('op/bigshow', searched);
            app.navigate(utils.composeQueryString('#op/bigshow/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('op/bigshow')
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#op/bigshow/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            utils.getPaginator(that.options, this.collection.total, '#op/bigshow/');
            return this;
        },
        load: function () {
            this.options.calc_show = 1;
            if (!this.options.target_amount) {
                this.options.target_amount = JSON.stringify({
                    '$gte': 2000
                });
            }
            this.options.current_term = JSON.stringify({
                "$gt": 1
            });
            this.options.status = 1;
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/BigShowList.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new BigShowListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });
    var BigShowDetailView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
        },

        initialize: function (options) {
            this.options = options;
            this.info = options.params || {};
            this.info.id = options.template_id;
            delete options.params;
            this.collection = new models.ActivityCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                info: this.info
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    that.collection.fetch({
                        reset: true,
                        data: that.options
                    })
                }
            });
            $('#paginator').bootstrapPaginator({
                currentPage: this.options.$page,
                totalPages: Math.ceil(this.collection.total / 10) || 1,
                size: 'small',
                numberOfPages: 10,
                alignment: 'center',
                onPageClicked: function (e, originalEvent, type, page) {
                    e.stopImmediatePropagation();
                    that.options.$page = page;
                    that.collection.fetch({
                        reset: true,
                        data: that.options
                    });
                }
            });
            return this;
        },
        load: function () {
            this.options.calc_show = 1;
            this.options.status = 4;
            if (!this.options.$orderby) this.options.$orderby = "-term_number";
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.Detail = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/BigShowDetail.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new BigShowDetailView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});